<script setup>
import { ElMessage } from 'element-plus';
import Layer from "@/components/layer/index.vue";
import { defineProps, ref, defineEmits, onMounted } from 'vue'
import { addAPI, updateAPI } from '@/api/wedding/serviceTypeInfo'
import { dicVals } from '@/api/common';

// 使用defineEmits来接收来自父组件的方法
const emit = defineEmits(['getTableData'])
// 声明props
const props = defineProps({
  layer: {
    type: Object,
    default: () => {
      return {
        show: false,
        title: '',
        showButton: true
      }
    }
  }
});

/************************************定义变量************************************************/
const ruleForm = ref(null)
const layerDom = ref(null)
const condition = ref(0);
//设置条件分类
const onChange = (value) => {
  console.log('[ value ] >', value)
  condition.value = value;
}
const rules = {
  type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
  name: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
}
let form = ref({
  id: null,
  type: null,
  format: null,
  priceKey: null,
  priceStart: null,
  priceEnd: null,
  userId: null,
  userName: null,
  createTime: null,
  updateTime: null,
  delFlag: null,
  condition: null,
})

/************************************数据回显************************************************/
if (props.layer.row) {
  if (props.layer.row) {
    condition.value = props.layer.row.condition || 0; // 设置条件分类的默认值
    let source = props.layer.row
    Object.keys(form.value).forEach(k => k in source && (form.value[k] = source[k]));
  }
}

/************************************定义事件************************************************/
//提交按钮事件
let submit = () => {
  if (ruleForm) {
    ruleForm.value.validate((valid) => {
      if (valid) {
        let params = form.value
        if (props.layer.row) {
          updateForm(params)
        } else {
          addForm(params)
        }
      } else {
        return false;
      }
    });
  }
}

let updateForm = (params) => {
  updateAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '编辑成功'
      })
      emit('getTableData', false)
      layerDom && layerDom.value.close()
    })
}

let addForm = (params) => {
  addAPI(params)
    .then(res => {
      ElMessage({
        type: 'success',
        message: '新增成功'
      })
      emit('getTableData', true)
      layerDom && layerDom.value.close()
    })
}


// 字典初始化start
const busTypeList = ref([]);
const initDicts = async () => {
  const res = await dicVals('bus_type');
  busTypeList.value = res.data;
  console.log('[ busTypeList ] >', busTypeList)
};
// 字典初始化end

/************************************生命周期************************************************/
//获取表格数据
onMounted(async () => {
  // vue的api
  await initDicts();
});
</script>

<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right:30px;">
      <el-form-item label="业务类型：" prop="type">
        <el-select v-model="form.type" placeholder="请选择业务类型">
          <el-option v-for="item in busTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="类型名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入类型名称"></el-input>
      </el-form-item>
      <el-form-item label="查询条件分类" prop="format">
        <t-radio-group default-value="0" @change="onChange">
          <t-radio value="0">设置款型条件</t-radio>
          <t-radio value="1">设置价格条件</t-radio>
        </t-radio-group>
      </el-form-item>
      <el-form-item v-if="condition == 0" label="服务款式/风格/规格：" prop="format">
        <el-input v-model="form.format" placeholder="请输入服务款式/风格/规格"></el-input>
      </el-form-item>
      <el-form-item v-if="condition == 1" label="区间开始：" prop="priceStart">
        <el-input type="number" v-model="form.priceStart" placeholder="请输入区间开始"></el-input>
      </el-form-item>
      <el-form-item v-if="condition == 1" label="区间结束：" prop="priceEnd">
        <el-input type="number" v-model="form.priceEnd" placeholder="请输入区间结束"></el-input>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<style scoped lang="scss">
.el-input-number--large {
  line-height: 38px;
  width: 230px;
}
</style>